from channels.generic.websocket import WebsocketConsumer
from channels.exceptions import StopConsumer
import json

class ChatingSocket(WebsocketConsumer):
    users = []
    def websocket_connect(self, message):
        print("有人来连接")
        self.accept()
        self.users.append(self)

    def websocket_receive(self, message):
        text = message['text']
        # print("你好，这里是ChatingSocket!")
        print(text)
        for user in self.users:
            user.send(text)

    def websocket_disconnect(self, message):
        print("断开连接")
        self.users.remove(self)
        raise StopConsumer()
